# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/8/14
import json
import re
from urllib.parse import unquote
import requests


def curl_to_dict(curl, curl_type="charles"):
    curl = curl.replace("\n", "")
    if curl_type == "charles":
        curl_dict = {"headers": {}, "params": {}, "data": {}, "cookies": {}}

        sqm = "'"
        dqm = '"'

        # 获取headers
        headers_patten = re.compile(f"-H {sqm}.*?{sqm}")
        headers_res = headers_patten.findall(curl)
        if len(headers_res) < 1:
            headers_patten = re.compile(f"-H {dqm}.*?{dqm}")
            headers_res = headers_patten.findall(curl)

        # 获取url_res
        url_patten = re.compile(f"--compressed {sqm}.*?{sqm}")
        url_res = url_patten.findall(curl)
        if len(url_res) < 1:
            url_patten = re.compile(f"--compressed {dqm}.*?{dqm}")
            url_res = url_patten.findall(curl)

        # 获取data
        if len(url_res) > 0:
            # 有--compressed的情况
            data_patten = re.compile(f"--data-binary {sqm}.*? --compressed")
            data_res = data_patten.findall(curl)
            if len(data_res) < 1:
                data_patten = re.compile(f"--data-binary {dqm}.*? --compressed ")
                data_res = data_patten.findall(curl)
            if len(data_res) > 0:
                data_res = data_res[0][15:][:-15].replace(" ", "")
        else:
            # 无--compressed的情况
            data_res = curl.split("--data-binary ")[-1].replace(" ", "")

        # 写入method
        if len(data_res) > 0:
            curl_dict["method"] = "POST"
        else:
            curl_dict["method"] = "GET"

        # 写入headers
        if len(headers_res) > 0:
            for item in headers_res:
                list_tmp = item[4:][:-1].split(': ')
                curl_dict["headers"][list_tmp[0]] = unquote(list_tmp[1])

        # 写入url
        if len(url_res) > 0:
            curl_dict["url"] = url_res[0][14:][:-1]

        # 写入path params
        if "url" in curl_dict.keys():
            list_tmp = curl_dict["url"].split(curl_dict["headers"]["Host"])[1].split("?")
            curl_dict["path"] = list_tmp[0]
            # 写入params
            if len(list_tmp) > 1:
                for item in list_tmp[1].split("&"):
                    list_tmp_inner = item.split("=")
                    curl_dict["params"][list_tmp_inner[0]] = unquote(list_tmp_inner[1])

        # 写入data
        if len(data_res) > 0:
            if "&" in data_res:
                for item in data_res[15:][:-15].split("&"):
                    list_tmp_inner = item.split("=")
                    if len(list_tmp_inner) > 1:
                        curl_dict["data"][list_tmp_inner[0]] = unquote(list_tmp_inner[1])
            else:
                curl_dict["data"]["data"] = data_res

        # 写入cookies
        if "Cookie" in curl_dict["headers"].keys():
            for item in curl_dict["headers"]["Cookie"][1:][:-1].split("; "):
                list_tmp_inner = item.split("=")
                curl_dict["cookies"][list_tmp_inner[0]] = list_tmp_inner[1]
        return curl_dict

    if curl_type == "chrome":
        pass


def curl_to_json(curl):
    curl_json = json.dumps(curl_to_dict(curl))
    return curl_json


if __name__ == '__main__':
    def test_curl_to_dict():
        curl_charles = '''
curl -H 'Host: sso.bytedance.com' -H 'Cookie: _ga=GA1.2.1206643526.1595330715; csrftoken=FLu4k161NushWmI5wiRwBieu4C8DjHIU; sessionid=x4414ja4ii2g9txa5sj4hh3xw9bks5c4; SLARDAR_WEB_ID=fa556c32-4ef1-4367-a65d-d5e0b629e375; callback.url=https://rms.bytedance.com/attendRecord; _gid=GA1.2.1346450479.1597751615; _ssa_userphone=; _ssa_username=hanjunfeng' -H 'cache-control: max-age=0' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36' -H 'accept: text/templates,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'sec-fetch-site: cross-site' -H 'sec-fetch-mode: navigate' -H 'sec-fetch-dest: document' -H 'accept-language: zh-CN,zh;q=0.9' -H 'if-none-match: W/"5f1d2ec4-6da"' -H 'if-modified-since: Sun, 26 Jul 2020 07:20:36 GMT' --compressed 'https://sso.bytedance.com/welcome'
'''
        curl_charles = '''
curl -H "Host: www.szse.cn" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "X-Request-Type: ajax" -H "X-Requested-With: XMLHttpRequest" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36" -H "Content-Type: application/json" -H "Referer: http://www.szse.cn/market/product/list/etfList/index.html" -H "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8" --compressed "http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1945&tab1PAGENO=1&random=0.9249280647700491"
'''
        curl_charles = '''
        curl -H "Host: api3-normal-lf.toutiaoapi.com" -H "Cookie: passport_csrf_token=1bc0bd8cfae81879783ad000af2f6a09; passport_csrf_token_default=1bc0bd8cfae81879783ad000af2f6a09; odin_tt=64b0cc9b651577c13af26f6a7ced42357148f92c8e1507d932a415e92c8213605e05eec00cad1bc1f8b6e97ee76e2d64689b6e96a794c8313e801f1f7e52104cb445ab0a44781fb38849ae7914d1648e; gftoken=MTA1MzgxNjU4NHwxNjc1ODI1Njc4MzF8fDAGBgYGBgY; store-region=cn-bj; store-region-src=did; install_id=805315781374701; ttreq=1$2470c261a283d0bb9b780884604a20bfbb595fbe" -H "accept: application/json" -H "x-vc-bdturing-sdk-version: 3.5.0" -H "content-type: application/json; encoding=utf-8" -H "x-ss-cookie: install_id=805315781374701; store-region=cn-bj; store-region-src=did; ttreq=1$2470c261a283d0bb9b780884604a20bfbb595fbe; FRM=new; PIXIEL_RATIO=2; WIN_WH=414_896; odin_tt=64b0cc9b651577c13af26f6a7ced42357148f92c8e1507d932a415e92c8213605e05eec00cad1bc1f8b6e97ee76e2d64689b6e96a794c8313e801f1f7e52104cb445ab0a44781fb38849ae7914d1648e; passport_csrf_token=1bc0bd8cfae81879783ad000af2f6a09; passport_csrf_token_default=1bc0bd8cfae81879783ad000af2f6a09" -H "tt-request-time: 1675948951325" -H "user-agent: News 9.1.7 rv:9.1.7.20 (iPhone; iOS 13.5.1; zh_CN) Cronet" -H "x-tt-request-tag: n=0;s=0" -H "sdk-version: 2" -H "x-tt-dt: AAA36ZYGPR4MX3QAL7HOE3X2D4ANIAOEEL3C3OGXLA34LSQZP5265R5MMKGKJ6VJCHSP2I7OCIVRRYE7NJVEDHHVZPSORTMIO5BFKZ5LU7YFO634J6RSFAE65QHRJMODPRQCMWVTSMLPYFUMV45Z6AA" -H "passport-sdk-version: 5.17.6-alpha.19-toutiao" -H "x-ss-stub: 6CEB588B288330B9049F87397786B70C" -H "x-tt-store-region: cn-bj" -H "x-tt-store-region-src: did" -H "x-ss-dp: 13" -H "x-tt-trace-id: 00-365788ab0db486ee658a207fda02000d-365788ab0db486ee-01" -H "x-argus: d0CFEk4BFws+IxyDW++oas2pNbgsZoXi+Px9Im4uLy3KQnXpkUt24+aUopygRPqC1mKIm7xY/4cLRnZzn1bXrxe96yzDEwW5znHsF3hpr4aAY7lSf3b3jLRcmVnHGtTdWRUh1t5dc1KbFYMtSBk1LzbGObzUcC4vfB9BZ5Z2pOPDdqcdW0XNktgnhwvW4X95VJJyCAsLYS9cIy6ud2McHGRYlN9TP/c3/CCPDkqT4dZpziEjObDwyhkVrTO9rFNzxiM=" -H "x-gorgon: 8404805600006022606cd61eb4701fe46dffd46eb108c42aab29" -H "x-helios: GgCSGFhfAj+qtL+KncjCj6Ds1B+c0LDO1n7piYUllXjbWVbG" -H "x-khronos: 1675948951" -H "x-ladon: /itTCTqaHW/GvutHIelgXXyC5GhU2ethThUVmAhSngc8qqd8" -H "x-medusa: lfPkY6GfcQ+igb74cyf5Bl9LnUX29pYSbkbNWVGx+h3AVtkMn9vECsyQev1a4vhFyzs1Vv/z9TTLHZ/QEO3qfcIdNWyJ5PyFvVHIYNdPsKpre7628ceVpHRKp5Q+rO9NyVgP2ZhUx3H85qQsZ6lTAf47DUONWYYgwMrqguk/YgR0cVhq8XtI/HYN3gCBPxNDOZ+KeJ4WSQHSGmD0bPPh7g2wmJjowGC/4Mwgvr7EOeqdY0kaR2I0fPiLqsZJCR950AY0QyLJCRU3n1HZLEPqC6LGUxbST/oAMZLlfgPtnU11FQoCalPyh7Mqdw9Z535Eb+je4zlUXjl9vd732bliaw1S9pCvig==" --data-binary "{
          \"trigger_type\" : \"first_launch\",
          \"uninterested_push_type\" : [

          ]
        }" --compressed "https://api3-normal-lf.toutiaoapi.com/cloudpush/pull_alter/notification_interest/?session_id=54204D4E-5124-4685-A166-0DEB37679825&version_code=9.1.7&tma_jssdk_version=2.53.0.23&app_name=news_article&app_version=9.1.7&device_id=3175865891987975&channel=App%20Store&resolution=828*1792&aid=13&ab_version=668775,4075552,5333126,5413934,5450464,5458734,5463176,5464423,662176,5450446,662099,5450412,668776,5450454,660830,5108071,5450462,5516454,1859936,668779,5450459,668774,5450453,4697194,4744162&ab_feature=794528,1662483,3408339,5222385&ab_group=794528,1662483,3408339,5222385&update_version_code=91720&cdid=171255E1-800C-4C37-9E3F-544A918DBC94&ac=WIFI&os_version=13.5.1&ssmix=a&device_platform=iphone&iid=805315781374701&device_type=iPhone%2011&ab_client=a1,f2,f7,e1"
            '''
        # curl_charles = '''
        # curl -H "Host: api3-normal-lf.toutiaoapi.com" -H "Cookie: passport_csrf_token=1bc0bd8cfae81879783ad000af2f6a09; passport_csrf_token_default=1bc0bd8cfae81879783ad000af2f6a09; odin_tt=64b0cc9b651577c13af26f6a7ced42357148f92c8e1507d932a415e92c8213605e05eec00cad1bc1f8b6e97ee76e2d64689b6e96a794c8313e801f1f7e52104cb445ab0a44781fb38849ae7914d1648e; gftoken=MTA1MzgxNjU4NHwxNjc1ODI1Njc4MzF8fDAGBgYGBgY; store-region=cn-bj; store-region-src=did; install_id=805315781374701; ttreq=1$2470c261a283d0bb9b780884604a20bfbb595fbe" -H "accept: application/json" -H "x-vc-bdturing-sdk-version: 3.5.0" -H "content-type: application/json; encoding=utf-8" -H "x-ss-cookie: install_id=805315781374701; store-region=cn-bj; store-region-src=did; ttreq=1$2470c261a283d0bb9b780884604a20bfbb595fbe; FRM=new; PIXIEL_RATIO=2; WIN_WH=414_896; odin_tt=64b0cc9b651577c13af26f6a7ced42357148f92c8e1507d932a415e92c8213605e05eec00cad1bc1f8b6e97ee76e2d64689b6e96a794c8313e801f1f7e52104cb445ab0a44781fb38849ae7914d1648e; passport_csrf_token=1bc0bd8cfae81879783ad000af2f6a09; passport_csrf_token_default=1bc0bd8cfae81879783ad000af2f6a09" -H "tt-request-time: 1675948951325" -H "user-agent: News 9.1.7 rv:9.1.7.20 (iPhone; iOS 13.5.1; zh_CN) Cronet" -H "x-tt-request-tag: n=0;s=0" -H "sdk-version: 2" -H "x-tt-dt: AAA36ZYGPR4MX3QAL7HOE3X2D4ANIAOEEL3C3OGXLA34LSQZP5265R5MMKGKJ6VJCHSP2I7OCIVRRYE7NJVEDHHVZPSORTMIO5BFKZ5LU7YFO634J6RSFAE65QHRJMODPRQCMWVTSMLPYFUMV45Z6AA" -H "passport-sdk-version: 5.17.6-alpha.19-toutiao" -H "x-ss-stub: 6CEB588B288330B9049F87397786B70C" -H "x-tt-store-region: cn-bj" -H "x-tt-store-region-src: did" -H "x-ss-dp: 13" -H "x-tt-trace-id: 00-365788ab0db486ee658a207fda02000d-365788ab0db486ee-01" -H "x-argus: d0CFEk4BFws+IxyDW++oas2pNbgsZoXi+Px9Im4uLy3KQnXpkUt24+aUopygRPqC1mKIm7xY/4cLRnZzn1bXrxe96yzDEwW5znHsF3hpr4aAY7lSf3b3jLRcmVnHGtTdWRUh1t5dc1KbFYMtSBk1LzbGObzUcC4vfB9BZ5Z2pOPDdqcdW0XNktgnhwvW4X95VJJyCAsLYS9cIy6ud2McHGRYlN9TP/c3/CCPDkqT4dZpziEjObDwyhkVrTO9rFNzxiM=" -H "x-gorgon: 8404805600006022606cd61eb4701fe46dffd46eb108c42aab29" -H "x-helios: GgCSGFhfAj+qtL+KncjCj6Ds1B+c0LDO1n7piYUllXjbWVbG" -H "x-khronos: 1675948951" -H "x-ladon: /itTCTqaHW/GvutHIelgXXyC5GhU2ethThUVmAhSngc8qqd8" -H "x-medusa: lfPkY6GfcQ+igb74cyf5Bl9LnUX29pYSbkbNWVGx+h3AVtkMn9vECsyQev1a4vhFyzs1Vv/z9TTLHZ/QEO3qfcIdNWyJ5PyFvVHIYNdPsKpre7628ceVpHRKp5Q+rO9NyVgP2ZhUx3H85qQsZ6lTAf47DUONWYYgwMrqguk/YgR0cVhq8XtI/HYN3gCBPxNDOZ+KeJ4WSQHSGmD0bPPh7g2wmJjowGC/4Mwgvr7EOeqdY0kaR2I0fPiLqsZJCR950AY0QyLJCRU3n1HZLEPqC6LGUxbST/oAMZLlfgPtnU11FQoCalPyh7Mqdw9Z535Eb+je4zlUXjl9vd732bliaw1S9pCvig==" --data-binary "{
        #   \"trigger_type\" : \"first_launch\",
        #   \"uninterested_push_type\" : [
        #   ]
        # }" '''
        curl_charles = """curl -H "Host: m.xflapp.com" -H "Cookie: MONITOR_WEB_ID=94f1a7f1-a21f-4a30-bc8b-c4c52c93eefc; tt_webid=7194352587184686651; x-jupiter-uuid=16759661712493392; Hm_lvt_037dd55b1ef5343db5f67c8fc95b6f53=1675336427,1675407388,1675966172,1675999641; Hm_lpvt_037dd55b1ef5343db5f67c8fc95b6f53=1675999641" -H "sec-ch-ua: \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"108\", \"Microsoft Edge\";v=\"108\"" -H "accept: application/json, text/plain, */*" -H "sec-ch-ua-mobile: ?1" -H "user-agent: Mozilla/5.0 (Linux; Android 12; M2102J2SC) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36 EdgA/108.0.1462.48" -H "sec-ch-ua-platform: \"Android\"" -H "sec-fetch-site: same-origin" -H "sec-fetch-mode: cors" -H "sec-fetch-dest: empty" -H "referer: https://m.xflapp.com/xiaoqu/bj-6587273935077769479.html?origin_search_id=20230210112723309C7D2A6911ED675E40&lm=ugtoutiao" -H "accept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7" --compressed "https://m.xflapp.com/f100/api/same_neighborhood_rent?neighborhood_id=6587273935077769479&city_id=122&offset=0&count=10&opt_pack=1&source=h5&is_h5_home_page=1&is_inner=0&origin_from=be_null&lm=ugtoutiao&platform=baidu"
        """
        curl_dict = curl_to_dict(curl_charles)
        curl_json = curl_to_json(curl_charles)

        print(curl_dict)
        # print(curl_json)


    test_curl_to_dict()
